package com.semidata.trp.monitor.main;

import com.semidata.rtd.core.datasource.redis.RedisDataSource;
import com.semidata.rtd.core.datasource.redis.RedisDataSourceService;
import com.semidata.rtd.core.executor.TaskExecutorService;
import com.semidata.rtd.core.executor.ThreadPoolTaskExecutor;
import com.semidata.rtd.core.http.server.HttpService;
import com.semidata.rtd.core.log.Logger;
import com.semidata.rtd.core.service.ServiceManager;
import com.semidata.trp.monitor.config.Config;
import com.semidata.trp.monitor.http.MonitorHttpDispatcher;
import com.semidata.trp.monitor.log.MonitorLogService;
import com.semidata.trp.monitor.report.ReportService;

public class Main {

	private static Config config;

	public static void main(String[] args) {

		String configFile;
		if (args.length > 0) {
			configFile = args[0];
		} else {
			configFile = null;
		}

		try {
			Config.init(configFile);
		} catch (RuntimeException e) {
			System.err.println("can not load config file");
			return;
		}

		config = Config.getConfig();
		ThreadPoolTaskExecutor threadpool = new ThreadPoolTaskExecutor();
		ServiceManager.register(new MonitorLogService());
		ServiceManager.register(new ReportService());
		ServiceManager.register(new RedisDataSourceService(Config.getConfig().getRedisHosts(), config.getRedisThreads(), config.getRedisConnectNum(), config.getRedisConnectTimeout()));
		ServiceManager.register(new TaskExecutorService(
				threadpool));
		ServiceManager.register(new HttpService(config.getHttpPort(), config
				.getHttpThreads(), new MonitorHttpDispatcher()));
		
		//config.setRedisDataSource(new RedisDataSource(config.getRedisHosts(), config.getRedisThreads(), config.getRedisConnectNum(), config.getRedisConnectTimeout()));
		ServiceManager.startAll();
		Logger.log("DEBUG", "monitor server started");
		ServiceManager.sync();
	}

}
